package com.itheima.factory;

import com.itheima.controller.IStudentController;
import com.itheima.dao.IStudentDao;
import com.itheima.service.IStudentService;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/*
工厂类
生产对象
写 配置文件
内部  接口和 实现类的对应关系
在Factory 中读配置文件
用配置文件中的数据 创建对象

spring  just soso

 */
public class Factory {
    private static Properties prop = new Properties();

    static {
        InputStream is = ClassLoader.getSystemResourceAsStream("bean.properties");
        try {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //生产 StudentController
    public static IStudentController getStudentController() {

        Object o = null;
        try {
            Class clazz = Class.forName(prop.getProperty("IStudentController"));
            o = clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return (IStudentController) o;
    }

    //生产 StudentService
    public static IStudentService getStudentService() {
        Object o = null;
        try {
            Class clazz = Class.forName(prop.getProperty("IStudentService"));
            o = clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (IStudentService) o;
    }

    //生产 StudentDao
    public static IStudentDao getStudentDao() {
        Object o = null;
        try {
            Class clazz = Class.forName(prop.getProperty("IStudentDao"));
            o = clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (IStudentDao) o;
    }

}
